package com.aspire.nm.component.cmppserver.runTime.moProcess;

import javax.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aspire.nm.component.cmppserver.filter.coder.packet.CmppDeliverPacket;
import com.aspire.nm.component.cmppserver.plugins.DeliverListener;
import com.aspire.nm.component.cmppserver.runTime.SpeedController;
import com.aspire.nm.component.cmppserver.runTime.SysRunTimeService;
import com.aspire.nm.component.cmppserver.runTime.UserRunTime;
import com.aspire.nm.component.cmppserver.runTime.moProcess.queue.MOQueue;
import com.aspire.nm.component.cmppserver.util.LogService;

@Scope("prototype")
@Component
public class MoLoader extends Thread implements InitializingBean{
	
	@Resource
	private MOQueue<CmppDeliverPacket> mOQueue;
	public MOQueue<CmppDeliverPacket> getmOQueue() {
		return mOQueue;
	}
	public void setmOQueue(MOQueue<CmppDeliverPacket> mOQueue) {
		this.mOQueue = mOQueue;
	}
	
	@Resource
	private SysRunTimeService sysRunTimeService;
	
	@Resource
	private SpeedController speedController;
	@Resource
	private LogService logService;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		start();
	}
	
	
	
	
	
	
	private UserRunTime userRunTime;
	private DeliverListener deliverListener;
	
	
    public DeliverListener getDeliverListener() {
        return deliverListener;
    }
    public void setDeliverListener(DeliverListener deliverListener) {
        this.deliverListener = deliverListener;
    }
    public UserRunTime getUserRunTime() {
		return userRunTime;
	}
	public void setUserRunTime(UserRunTime userRunTime) {
		this.userRunTime = userRunTime;
	}
	
	
	
	
	
	
	
	public void run(){
		while (true) {
			if(deliverListener == null || !userRunTime.haveMoConn()){
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}
			try {
				if(deliverListener != null){
				    //上行限速
				    int moLimitInSec = userRunTime.getClientConfig().getMoLimitInSec();
				    if(moLimitInSec != 0){
				        speedController.isTimeLimitInSecond(moLimitInSec, true);
				    }
					CmppDeliverPacket cmppDeliverPacket = deliverListener.getDeliver(userRunTime.getUser());
					if(cmppDeliverPacket != null){
						mOQueue.put(cmppDeliverPacket);
						logService.deliver(userRunTime.getUser(), cmppDeliverPacket);
						if(cmppDeliverPacket.isSMSReport()){
							sysRunTimeService.getMoMtCounter().getReportCounter().increment();
							userRunTime.getMoMtCounter().getReportCounter().increment();
						}else{
							sysRunTimeService.getMoMtCounter().getMoCounter().increment();
							userRunTime.getMoMtCounter().getMoCounter().increment();
						}
						continue;
					}
				}
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}
